<template>
  <div v-if="!LOGIN" class="login-container">
    <h2>登录</h2>
    <el-form :model="loginForm" @submit.prevent="handleLogin" @keyup.enter="handleLogin">
      <el-form-item label="用户名">
        <el-input v-model="loginForm.username"></el-input>
      </el-form-item>
      <el-form-item label="密码">
        <el-input type="password" v-model="loginForm.password"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="handleLogin">登录</el-button>
      </el-form-item>
    </el-form>
  </div>
  <div v-else class="common-layout">
    <el-container style="height: 100%;">
      <el-header>
        <span>灰飞虱检测和轨迹跟踪系统</span>
      </el-header>
      <el-container>
        <el-aside width="200px">
          <el-menu :default-active="activeMenuItem" router>
            <!-- <el-menu-item index="/main">主界面</el-menu-item> -->
            <el-menu-item index="/imageSelect">图片检测</el-menu-item>
            <el-menu-item index="/imageHistory">图片历史</el-menu-item>
            <el-menu-item index="/videoSelect">视频跟踪</el-menu-item>
            <el-menu-item index="/videoHistory">视频历史</el-menu-item>
          </el-menu>
        </el-aside>
        <el-main>
          <el-scrollbar>
            <router-view></router-view>
          </el-scrollbar>
        </el-main>
      </el-container>
    </el-container>
  </div>
</template>

<script setup lang="ts">
import { ref, onMounted, watch } from 'vue'
import { useRouter } from 'vue-router'
import { LOGIN, SERVER_URL } from './components/handleApp'
import axios from 'axios'
import { ElMessage } from 'element-plus'

const loginForm = ref({
  username: '',
  password: ''
})

const router = useRouter()
const activeMenuItem = ref('')

function handleLogin() {
  axios.get(`${SERVER_URL}/login/`, {
    params: {
      username: loginForm.value.username,
      password: loginForm.value.password
    }
  })
  .then(res => {
    if (res.data && res.data.length > 0) {
      LOGIN.value = true
      window.sessionStorage.setItem('username', loginForm.value.username)
      window.sessionStorage.setItem('password', loginForm.value.password)
    }
    else {
      ElMessage.error('用户名或密码错误')
      LOGIN.value = false
    }
  })
}
watch(router.currentRoute, (newVal) => {
  const path = newVal.path

  if (path.startsWith("/detect")) {
    activeMenuItem.value = "/imageSelect";
  }
  else if (path.startsWith("/track")) {
    activeMenuItem.value = '/videoSelect'
  }
  else {
    activeMenuItem.value = path;
  }
})

onMounted(() => {
  LOGIN.value = false
  console.log("app mounted!");
  if (window.sessionStorage.getItem('username') && window.sessionStorage.getItem('password')) {
    loginForm.value.username = window.sessionStorage.getItem('username') || ''
    loginForm.value.password = window.sessionStorage.getItem('password') || ''
    handleLogin()
  }
});
</script>


<style scoped>
.el-header {
  display: flex;
  flex-direction: row-reverse;
  align-items: center;
  gap: 20px;
  background-color: aquamarine;
}

.el-menu-item {
  background-color: blanchedalmond;
  margin-bottom: 5px;
  border-radius: 10px;
}

.el-main {
  width: 100%;
  height: 660px;
  --el-main-padding: 10px;
}

.login-container {
  max-width: 300px;
  margin: 50px auto;
  padding: 20px;
  border: 1px solid #ccc;
  border-radius: 10px;
  background-color: #f9f9f9;
  display: flex;
  flex-direction: column;
  align-items: center;
}

.el-form-item {
  width: 100%;
  max-width: 250px;
}

.el-input {
  width: 100%;
}
</style>
